using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class EffectItem : BaseItem
	{
		public enum State
		{
			Hide = 0,
			Show = 1
		}

		private Transform effect;

		private State state;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			SetState((State)data.state);
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (state == State.Hide)
			{
				SetState(State.Show);
			}
			else
			{
				SetState(State.Hide);
			}
		}

		private void SetState(State val)
		{
			if (state != val)
			{
				state = val;
				if (state == State.Hide)
				{
					SetEffectLoop(effect, false);
				}
				else
				{
					ShowEffect();
				}
			}
		}

		private void ShowEffect()
		{
			if (string.IsNullOrEmpty(base.itemData.effect))
			{
				return;
			}
			if (effect == null)
			{
				Transform transform = model.Find("Efc");
				if (transform != null)
				{
					transform.SetLocalZ(-0.0004f);
					effect = Singleton<GamePoolManager>.Instance.Spawn(PathConst.EFFECT + base.itemData.effect);
					if (effect != null)
					{
						effect.SetParent(transform);
						effect.localPosition = Vector3.zero;
						effect.localScale = Vector3.one;
					}
				}
			}
			SetEffectLoop(effect, true);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
